package com.example.behaviorpattern.iterator.impl;

import com.example.behaviorpattern.iterator.Aggregate;
import com.example.behaviorpattern.iterator.Iterator;

/**
 * 具体聚合实现
 *
 * @author yaojianfeng
 */
public class ConcreteAggregate implements Aggregate {
    @lombok.Getter
    private Object[] items;
    private int size = 0;

    public ConcreteAggregate(int capacity) {
        items = new Object[capacity];
    }

    public Object get(int index) {
        return items[index];
    }

    public void add(Object obj) {
        items[size++] = obj;
    }

    public int size() {
        return size;
    }

    @Override
    public Iterator createIterator() {
        return new ConcreteIterator(this);
    }
}